//
//  NSString+handle.m
//  OMS
//
//  Created by goldeney on 14/12/1.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "NSString+handle.h"

@implementation NSString (handle)

/**
 @method 获取指定宽度情况ixa，字符串value的高度
 @param value 待计算的字符串
 @param fontSize 字体的大小
 @param andWidth 限制字符串显示区域的宽度
 @result float 返回的高度
 */
- (float)heightForFont:(UIFont*)font andWidth:(float)width
{
    CGSize sizeToFit = [self sizeWithFont:font constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                            lineBreakMode:NSLineBreakByWordWrapping];//此处的换行类型（lineBreakMode）可根据自己的实际情况进行设置
    return sizeToFit.height;
}

- (float)widthForFont:(UIFont*)font andHeight:(float)height
{
    CGSize sizeToFit = [self sizeWithFont:font constrainedToSize:CGSizeMake(CGFLOAT_MAX, height)
                            lineBreakMode:NSLineBreakByWordWrapping];//此处的换行类型（lineBreakMode）可根据自己的实际情况进行设置
    return sizeToFit.width;
}

- (CGFloat)heightAutoCalculateFontSize:(UIFont*)font andWidth:(float)width
{
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
    
    NSDictionary* attributes =@{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    return labelSize.height;
}

- (CGFloat)widthAutoCalculateFontSize:(UIFont*)font andHeight:(float)height
{
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
    
    NSDictionary* attributes =@{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
    CGSize labelSize = [self boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    return labelSize.width;
}

@end
